//
//  NSString+Helper.m
//
//  Created  on 13-11-28.
//

#import "NSString+Helper.h"

@implementation NSString (Helper)

#pragma mark - 日期处理

//返回*月*日
+ (NSString *)stringThatDay:(NSDate *)day{
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"MM'月'dd"];
    NSString * locationString=[dateformatter stringFromDate:day];
    return locationString;
}

//返回当前日期
+ (NSString *)stringTime:(NSDate *)startDate{
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd"];
    NSString * locationString=[dateformatter stringFromDate:startDate];
    return locationString;
}

//返回某日的后七天
+ (NSString *)stringWeek:(NSDate *)startDate{
    NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60*6 sinceDate:startDate];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd"];
    NSString * locationString=[dateformatter stringFromDate:lastDay];
    return locationString;
}

//返回某日的后29天
+ (NSString *)stringMonth:(NSDate *)startDate{
    NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60*28 sinceDate:startDate];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd"];
    NSString * locationString = [dateformatter stringFromDate:lastDay];
    return locationString;
}

//字符串转date
+ (NSDate *)stringToDate:(NSString *)strDate{
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    [inputFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate* inputDate = [inputFormatter dateFromString:strDate];
    return inputDate;
}


+ (NSMutableArray *)getArrayDates:(NSDate *)startDate type:(NSString *)str{
    NSMutableArray *arrDares =[NSMutableArray array];
    int day;
    if ([str isEqualToString:@"周"]) {
        day = 7;
    }else{
        day = 29;
    }
    for (int i = 0; i < day; i++) {
        NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60*i sinceDate:startDate];
        NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
        [dateformatter setDateFormat:@"YYYY-MM-dd"];
        NSString * locationString = [dateformatter stringFromDate:lastDay];
        NSArray *arr = [locationString componentsSeparatedByString:@"-"];
        [arrDares addObject:[NSString stringWithFormat:@"%@/%@",arr[1],arr[2]]];
    }
    return arrDares;
}

+ (NSMutableArray *)getArrayDatesformat:(NSDate *)startDate type:(NSString *)str{
    NSMutableArray *arrDares =[NSMutableArray array];
    int day;
    if ([str isEqualToString:@"周"]) {
        day = 7;
    }else{
        day = 29;
    }
    for (int i = 0; i < day; i++) {
        NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60*i sinceDate:startDate];
        NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
        [dateformatter setDateFormat:@"YYYY-MM-dd"];
        NSString *locationString = [dateformatter stringFromDate:lastDay];
        [arrDares addObject:locationString];
    }
    return arrDares;
}

#pragma mark 清空字符串中的空白字符
- (NSString *)trimString
{
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

#pragma mark 是否空字符串
- (BOOL)isEmptyString
{
    return (self == nil || self.length == 0);
}

#pragma mark 返回沙盒中的文件路径
- (NSString *)documentsPath
{
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    return [path stringByAppendingString:self];
}

#pragma mark 写入系统偏好
- (void)saveToNSDefaultsWithKey:(NSString *)key
{
    [[NSUserDefaults standardUserDefaults] setObject:self forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)getFromNSDefaultsWithKey:(NSString *)key
{
    [[NSUserDefaults standardUserDefaults] objectForKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end
